using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUIPlayerChat : SelectableButton
	{
		public PlayerInfo playerInfo;

		public UILabel status;

		public new UILabel name;

		public UIBasicSprite skin;

		public UILabel newMessagesCount;

		public Color onlineColor;

		public Color offlineColor;

		public GameObject onlineLight;

		public GameObject offlineLight;

		public GameObject inOneRoomLight;

		public GameObject buttonInviteToBattle;

		private void OnEnable()
		{
			FriendsController.OnReciveNewMessages += UpdateNotification;
			InOneRoomView();
		}

		private void OnDisable()
		{
			FriendsController.OnReciveNewMessages -= UpdateNotification;
		}

		private void OnDestroy()
		{
			FriendsController.OnReciveNewMessages -= UpdateNotification;
		}

		public void Init(PlayerInfo playerInfo)
		{
			this.playerInfo = playerInfo;
			status.text = ((!playerInfo.isOnline) ? "Offline" : "Online");
			status.color = ((!playerInfo.isOnline) ? offlineColor : onlineColor);
			IsOnlineView();
			name.text = playerInfo.name;
			if (!skin.mainTexture.name.Equals("render_" + playerInfo.skinId))
			{
				skin = skin.ReplaceForCurrentPlatform();
				skin.LoadTextureAndAlphaMask("AtlasShopGuns/", "render_" + playerInfo.skinId);
			}
			id = playerInfo.playerId;
			uint value;
			if (FriendsController.instance.GetCountNewMessages().TryGetValue(id, out value))
			{
				ShowNotification(value);
			}
			else
			{
				ShowNotification(0u);
			}
		}

		public void ShowChat()
		{
			FriendsUIChat.GetInstance().messageScroll.ShowChat(playerInfo);
			ShowNotification(0u);
		}

		public void ShowNotification(uint count)
		{
			if (count == 0)
			{
				newMessagesCount.gameObject.SetActive(false);
				return;
			}
			newMessagesCount.text = count.ToString();
			newMessagesCount.gameObject.SetActive(true);
		}

		public override void Select(bool select)
		{
			if (selected != select)
			{
				if (select)
				{
					FriendsController.OnReciveNewMessages -= UpdateNotification;
				}
				else
				{
					FriendsController.OnReciveNewMessages += UpdateNotification;
				}
				if ((bool)buttonInviteToBattle)
				{
					buttonInviteToBattle.SetActive(select && CanSendInvite());
				}
			}
			base.Select(select);
		}

		public void InviteToBattle()
		{
			if (!CanSendInvite())
			{
				return;
			}
			FriendsController.instance.IdsFrinedsInviteToBattle.Add(playerInfo.playerId);
			FriendsUIGame.instance.curInviteToBattlePlayer--;
			FriendsController.instance.InviteFriendToBattle(new List<string> { playerInfo.playerId }, delegate(ServerResponse x)
			{
				if (x == ServerResponse.Successful)
				{
					FriendsUINotification.instance.AddInQueueWarninMsg(ScriptLocalization.Get("Friends/src_Notification_Succesful"));
				}
				else
				{
					FriendsUINotification.instance.AddInQueueWarninMsg(ScriptLocalization.Get("Friends/src_Notification_ServerFail"));
				}
			});
			buttonInviteToBattle.SetActive(false);
		}

		private bool CanSendInvite()
		{
			return (bool)FriendsUIGame.instance && FriendsUIGame.instance.CanSendInviteToBattle() && !FriendsController.instance.IdsFrinedsInviteToBattle.Contains(playerInfo.playerId) && !CheckInOneRoom();
		}

		private void UpdateNotification(Dictionary<string, uint> dictMsg)
		{
			uint value;
			if (dictMsg.TryGetValue(id, out value))
			{
				ShowNotification(value);
			}
		}

		private void IsOnlineView()
		{
			if ((bool)onlineLight && (bool)offlineLight)
			{
				onlineLight.SetActive(playerInfo.isOnline);
				offlineLight.SetActive(!playerInfo.isOnline);
			}
		}

		private void InOneRoomView()
		{
			if ((bool)inOneRoomLight)
			{
				inOneRoomLight.SetActive(CheckInOneRoom());
			}
		}

		private bool CheckInOneRoom()
		{
			return (bool)GameController.instance && GameController.instance.listPlayers.Exists((PlayerBehavior x) => x.props.playerId == playerInfo.playerId);
		}
	}
}
